package com.daoImpl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.LockMode;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.bean.CostcheckProduct;
import com.bean.CraftShow;
import com.dao.CostcheckProductDao;

/**
 * A data access object (DAO) providing persistence and search support for
 * CostcheckProduct entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see com.bean.CostcheckProduct
 * @author MyEclipse Persistence Tools
 */
public class CostcheckProductDaoImpl extends HibernateDaoSupport implements CostcheckProductDao {
	private static final Logger log = LoggerFactory
			.getLogger(CostcheckProductDaoImpl.class);
	// property constants
	public static final String PRODUCT_CODE = "productCode";
	public static final String PRICE = "price";
	public static final String SYS_USER = "sysUser";
	public static final String MEMO = "memo";

	protected void initDao() {
		// do nothing
	}

	/* (non-Javadoc)
	 * @see com.daoImpl.CostcheckProductDao#save(com.bean.CostcheckProduct)
	 */
	@Override
	public void save(CostcheckProduct transientInstance) {
		log.debug("saving CostcheckProduct instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.daoImpl.CostcheckProductDao#delete(com.bean.CostcheckProduct)
	 */
	@Override
	public void delete(CostcheckProduct persistentInstance) {
		log.debug("deleting CostcheckProduct instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.daoImpl.CostcheckProductDao#findById(java.lang.Integer)
	 */
	@Override
	public CostcheckProduct findById(java.lang.Integer id) {
		log.debug("getting CostcheckProduct instance with id: " + id);
		try {
			CostcheckProduct instance = (CostcheckProduct) getHibernateTemplate()
					.get("com.bean.CostcheckProduct", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.daoImpl.CostcheckProductDao#findByExample(com.bean.CostcheckProduct)
	 */
	@Override
	public List findByExample(CostcheckProduct instance) {
		log.debug("finding CostcheckProduct instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.daoImpl.CostcheckProductDao#findByProperty(java.lang.String, java.lang.Object)
	 */
	@Override
	public List findByProperty(String propertyName, Object value) {
		log.debug("finding CostcheckProduct instance with property: "
				+ propertyName + ", value: " + value);
		try {
			String queryString = "from CostcheckProduct as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.daoImpl.CostcheckProductDao#findByProductCode(java.lang.Object)
	 */
	@Override
	public List findByProductCode(Object productCode) {
		return findByProperty(PRODUCT_CODE, productCode);
	}

	/* (non-Javadoc)
	 * @see com.daoImpl.CostcheckProductDao#findByPrice(java.lang.Object)
	 */
	@Override
	public List findByPrice(Object price) {
		return findByProperty(PRICE, price);
	}

	/* (non-Javadoc)
	 * @see com.daoImpl.CostcheckProductDao#findBySysUser(java.lang.Object)
	 */
	@Override
	public List findBySysUser(Object sysUser) {
		return findByProperty(SYS_USER, sysUser);
	}

	/* (non-Javadoc)
	 * @see com.daoImpl.CostcheckProductDao#findByMemo(java.lang.Object)
	 */
	@Override
	public List findByMemo(Object memo) {
		return findByProperty(MEMO, memo);
	}

	/* (non-Javadoc)
	 * @see com.daoImpl.CostcheckProductDao#findAll()
	 */
	@Override
	public List findAll() {
		log.debug("finding all CostcheckProduct instances");
		try {
			String queryString = "from CostcheckProduct";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.daoImpl.CostcheckProductDao#merge(com.bean.CostcheckProduct)
	 */
	@Override
	public CostcheckProduct merge(CostcheckProduct detachedInstance) {
		log.debug("merging CostcheckProduct instance");
		try {
			CostcheckProduct result = (CostcheckProduct) getHibernateTemplate()
					.merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.daoImpl.CostcheckProductDao#attachDirty(com.bean.CostcheckProduct)
	 */
	@Override
	public void attachDirty(CostcheckProduct instance) {
		log.debug("attaching dirty CostcheckProduct instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.daoImpl.CostcheckProductDao#attachClean(com.bean.CostcheckProduct)
	 */
	@Override
	public void attachClean(CostcheckProduct instance) {
		log.debug("attaching clean CostcheckProduct instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
	
	/**
	 * 显示核价dao
	 */
	public List<CostcheckProduct> getFindCostcheckProduct(String table,String pk,String sort,int PageNumber,int PageSize ,String options){
		
		Session session = getSession();  
		SQLQuery query=null;
		
		StringBuffer sqlbuf = new StringBuffer("execute [dbo].[sp_get_page_of_table2] ");
		sqlbuf.append("'"+table+"','"+pk+"','"+sort+"',"+PageNumber+","+PageSize+",'*',\""+options+"\"");
		
		query=session.createSQLQuery(sqlbuf.toString());
		query.addEntity(CostcheckProduct.class);
		 
		List<CostcheckProduct> list = query.list();
		
		return list;
	}
	
	
	public static CostcheckProductDao getFromApplicationContext(
			ApplicationContext ctx) {
		return (CostcheckProductDao) ctx.getBean("CostcheckProductDaoImpl");
	}
}